#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/fs.h>
#include<linux/slab.h>
#include<linux/timer.h>
#include<linux/sched.h>
#include<linux/list.h>
#include<linux/interrupt.h>

#include<linux/cdev.h> //cdev_init() cdev_add() cdev_del()
#include<linux/types.h> //dev_t
#include<linux/kdev_t.h> //有两个宏获取主设备号和次设备号

MODULE_AUTHOR("Tan xujia");
MODULE_LICENSE("Dual BSD/GPL");
dev_t c_devt;  //设备的主次设备号
/*定义一个结构体，包含了cdev,类似与继承，把这个设备文件的属性给继承过来*/
struct char_dev {
    struct cdev c_dev;
    char c;
};
/*定义一个变量*/
struct char_dev *cdev_p;

/*设备的操作方法*/
int
char_open(struct inode *inode, struct file *filp)
{
    printk("char_open, %d, %d\n", iminor(inode), MINOR(inode->i_cdev->dev));
    return 0;
}

ssize_t
char_read(struct file *filp, char __user *buf, size_t count, loff_t *fpos)
{
    printk("char_read\n");
    return 0;
}

ssize_t
char_write(struct file *filp, const char __user *buf, size_t count, loff_t *fpos)
{
    printk("char_write\n");
    return count;
}

int
char_release(struct inode *inode, struct file *filp)
{
    printk("char_release\n");
    return 0;
}

struct file_operations char_fops = {
    .owner = THIS_MODULE,
    .read = char_read,
    .write = char_write,
    .open = char_open,
    .release = char_release,
};

struct class *cdev_class;

static
int __init hello_init (void)
{
    printk("hello_init\n");
    int ret = 0, index = 0;
    ret = alloc_chrdev_region(&c_devt, 10, 2, "char_dev");
    if(ret)
         printk("alloc_chrdev_region fail\n");

    printk("major = %d, minor = %d\n", MAJOR(c_devt), MINOR(c_devt));

    cdev_p = kzalloc(sizeof(struct char_dev) * 2, GFP_KERNEL);
    if(!cdev_p)
        printk("alloc fail\n");

    for(index = 0; index < 2; index++) {
        cdev_init(&cdev_p[index].c_dev, &char_fops);
        cdev_p[index].c_dev.owner = THIS_MODULE;
        ret = cdev_add(&cdev_p[index].c_dev, 
            MKDEV(MAJOR(c_devt), MINOR(c_devt) + index), 1);
        if(ret) 
            printk("cdev_add fail\n");
    }
    /*根据以下api替代mknod生成设备节点*/
    cdev_class = class_create(THIS_MODULE, "c_dev");
    if(!cdev_class)
        printk("class_create fail\n");

    for(index = 0; index < 2; index++) {
        device_create(cdev_class, NULL, 
            MKDEV(MAJOR(c_devt), MINOR(c_devt) + index), 
            NULL, "c_dev%d", index);
    }
    return 0;
}

static
void __exit hello_exit (void)
{
    printk("hello_exit\n");
    int index = 0;
    for(index = 0; index < 2; index++) {
        device_destory(cdev_class, 
            MKDEV(MAJOR(c_devt), MINOR(c_devt) + index));
    }

    class_destory(cdev_class);

    for(index = 0; index < 2; index++) {
         cdev_del(&cdev_p[index].c_dev);
    }

    kfree(cdev_p);

    unregister_chrdev_region(c_devt, 2);
}

module_init(hello_init);
module_exit(hello_exit);


/*
MAJOR(dev_t dev) //获取主设备号
MINOR(dev_t dev) //获取次设备号

MKDEV(int major, int minor) //合成设备号

设备号的分配：
int register_chrdev_region(dev_t first, unsigned int count,
    char *name)
int alloc_chrdev_region(dev_t *dev, unsigned int firstminor,
    unsigned int count, char *name)

设备号的释放：
void unregister_chrdev_region(dev_t first, unsigned int count)
*/